using System;
using System.Collections.Generic;
using System.Text;
using TapiRex;
using System.Diagnostics;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;
using System.Threading;
namespace mceTapiRexv2
{
    class TapiRexListener
    {
        public mceTapiRexv2.Application mceTapiRexv2;
        public IncomingCallListener inListener;
        public OutgoingCallEventListener outListener;
        public ExitEventListener exListener;
        public CallDisconnectListener discListener;
        public CallAnsweredListener ansListener;

        public class IncomingCallListener
        {
            private mceTapiRexv2.Application mceTapiRexv2;
            public IncomingCallListener(mceTapiRexv2.Application tRexApp)
            {
                this.mceTapiRexv2 = tRexApp;
                // Add "ListChanged" to the Changed event on "List".
                mceTapiRexv2.TapiRexApplication.evtIncomingCall += new __Application_evtIncomingCallEventHandler(IncomingCall);
                //mceTapiRexv2.TapiRexApplication.evtIncomingCall += new __Application_evtIncomingCallEventHandler(dump);
            }

            // This will be called whenever the list changes.
            private void IncomingCall(int id)
            {
                //Phonecall phonecall = mceTapiRexv2.getPhonecall(id);
                Thread.Sleep(500);
                CallMember cm = mceTapiRexv2.getCaller(id);

                object[] buttons = new object[] { };
                 
                AddInHost.Current.MediaCenterEnvironment.DialogNotification(cm.DisplayName,
                   buttons, 10, cm.PicturePath, new DialogClosedCallback(OnButtnClick));
            }
                    public void OnButtnClick(Microsoft.MediaCenter.DialogResult buttn)
                    {
                    }
            public void dump(int i)
            {
                Debug.WriteLine("Incoming CALL");
            }
             

            public void Detach()
            {
                // Detach the event and delete the list
                mceTapiRexv2.TapiRexApplication.evtIncomingCall -= new __Application_evtIncomingCallEventHandler(IncomingCall);
            }


        }
        public class OutgoingCallEventListener
        {
            private mceTapiRexv2.Application mceTapiRexv2;
            public OutgoingCallEventListener(mceTapiRexv2.Application tRexApp)
            {
                this.mceTapiRexv2 = tRexApp;
                mceTapiRexv2.TapiRexApplication.evtOutgoingCall += new __Application_evtOutgoingCallEventHandler(OutgoingCall);
            }
            private void OutgoingCall(int callid)
            {
                Debug.WriteLine("Outgoing call to number : " + callid);
            }
            private void Detach()
            {
                mceTapiRexv2.TapiRexApplication.evtOutgoingCall -= new __Application_evtOutgoingCallEventHandler(OutgoingCall);
            }
        }
        public class ExitEventListener
        {
            private mceTapiRexv2.Application mceTapiRexv2;
            public ExitEventListener(mceTapiRexv2.Application tRexApp)
            {
                this.mceTapiRexv2 = tRexApp;
                mceTapiRexv2.TapiRexApplication.evtExit += new __Application_evtExitEventHandler(ExitTapiRex);
            }
            public void OnButtnClick(Microsoft.MediaCenter.DialogResult buttn)
            {
            }
            private void ExitTapiRex()
            {
                object[] buttons = new object[] { };

                AddInHost.Current.MediaCenterEnvironment.DialogNotification("TapiRex exited",
                   buttons, 3, "C:\\Scendix\\mceTapiRexv2CP\\Images\\scendix.gif", new DialogClosedCallback(OnButtnClick));
            }
            private void Detach()
            {
                mceTapiRexv2.TapiRexApplication.evtExit -= new __Application_evtExitEventHandler(ExitTapiRex);
            }
        }
        public class CallDisconnectListener
        {
            private mceTapiRexv2.Application mceTapiRexv2;
            public CallDisconnectListener(mceTapiRexv2.Application tRexApp)
            {
                this.mceTapiRexv2 = tRexApp;
                mceTapiRexv2.TapiRexApplication.evtCallDisconnected += new __Application_evtCallDisconnectedEventHandler(CallDisconnect);
            }
            private void CallDisconnect(int id)
            {
                Debug.WriteLine("Call with id : " + id + " disconnected");
            }
            private void Detach()
            {
                mceTapiRexv2.TapiRexApplication.evtCallDisconnected -= new __Application_evtCallDisconnectedEventHandler(CallDisconnect);
            }
        }
        public class CallAnsweredListener
        {
            private mceTapiRexv2.Application mceTapiRexv2;
            public CallAnsweredListener(mceTapiRexv2.Application tRexApp)
            {
                this.mceTapiRexv2 = tRexApp;
                mceTapiRexv2.TapiRexApplication.evtCallAnswered += new __Application_evtCallAnsweredEventHandler(CallAnswered);
            }
            private void CallAnswered(int id)
            {
                Debug.WriteLine("Call with id : " + id + " answered");
            }
            private void Detach()
            {
                mceTapiRexv2.TapiRexApplication.evtCallAnswered -= new __Application_evtCallAnsweredEventHandler(CallAnswered);
            }

        }


        public TapiRexListener(mceTapiRexv2.Application app)
        {
            mceTapiRexv2 = app;
            inListener = new IncomingCallListener(app);
            outListener = new OutgoingCallEventListener(app);
            exListener = new ExitEventListener(app);
            discListener = new CallDisconnectListener(app);
            ansListener = new CallAnsweredListener(app);

        }

    }


}

